package com.dmooo.ymx.activity;

import android.hardware.Camera;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.king.zxing.CaptureHelper;
import com.king.zxing.OnCaptureCallback;
import com.king.zxing.ViewfinderView;
import com.dmooo.ymx.R;
import com.dmooo.ymx.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyScanActivity extends BaseActivity implements OnCaptureCallback {
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.sw_light)
    Switch aSwitch;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.surfaceView)
    SurfaceView surfaceView;
    @BindView(R.id.viewfinderView)
    ViewfinderView viewfinderView;
    private boolean isContinuousScan;
    private CaptureHelper mCaptureHelper;
    public static final String KEY_IS_CONTINUOUS = "key_continuous_scan";
    @Override
    protected void initUI() {
        setContentView(R.layout.activity_my_scan);
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        tvTitle.setText("扫码领券");
        tvLeft.setVisibility(View.VISIBLE);
        isContinuousScan = getIntent().getBooleanExtra(KEY_IS_CONTINUOUS,false);

        mCaptureHelper = new CaptureHelper(this,surfaceView,viewfinderView);
        mCaptureHelper.setOnCaptureCallback(this);
        mCaptureHelper.onCreate();
        mCaptureHelper.vibrate(true)
                .fullScreenScan(true)//全屏扫码
                .supportVerticalCode(true)//支持扫垂直条码，建议有此需求时才使用。
                .continuousScan(isContinuousScan);
        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    openFlash();
                }else{
                    offFlash();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCaptureHelper.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mCaptureHelper.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if(aSwitch.isChecked()){
//            offFlash();
//        }
        mCaptureHelper.onDestroy();
    }
    private void offFlash(){
        Camera camera = mCaptureHelper.getCameraManager().getOpenCamera().getCamera();
        Camera.Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
    }

    public void openFlash(){
        Camera camera = mCaptureHelper.getCameraManager().getOpenCamera().getCamera();
        Camera.Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mCaptureHelper.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
    @Override
    protected void initListener() {

    }
    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public boolean onResultCallback(String result) {
        return false;
    }
}
